<?php
	if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Articles_Ctrl extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
			}
		function index($query_id=0, $order_by  = 'publish_date', $sort_order = 'asc', $current_page = 0){
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					}
				}
			
			$results_per_page = 4;
			
			$url = CURL_URL;
			$data = array('www-command'=>'articles-get_articles','current_page'=>$current_page,'results_per_page'=>$results_per_page,'sort_order'=>$sort_order,'order_by'=>$order_by,'search_by'=>$search_by,'search_by_value'=>$search_by_value);
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response = json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
			if($response->message !== 'No Articles'){
			$this->load->library('pagination');			
			$config['base_url'] = 'http://localhost/azindia/azindia-client/index.php/admin/articles_ctrl/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['articles_get_articles'] = $response;
			$data['error'] = '';
			}else{
				$data['error'] = 1;
				$data['articles_get_articles'] = '';
			}
			//echo '<pre>';print_r($data);exit;
			$data['articles_fileds'] = array('article_title'=>'Article Title', 'article_type'=>'Article Type', 'author_name'=>'Author Name','publish_date'=>'Publish Date','status'=>'Status');
			$data['order_by'] = $order_by;
			$data['sort_order'] = $sort_order;
			$data['query_id'] = $query_id;
			$data['status'] = array(1=>'Active', 0=>'In-active');
			$data['search_by'] = $search_by;
			$data['search_by_value'] = $search_by_value;
			//echo '<pre>';print_r($response);exit;
			$this->load->view('admin/articles_view',$data);
		}
		
		function search_by(){
			//echo $this->input->post('status');exit;
			if($this->input->post('search') == '' && $this->input->post('search_date') == ''){
				$search = $this->input->post('status');
			}elseif($this->input->post('search') == '' && $this->input->post('status') == ''){
				$search = $this->input->post('search_date');
			}else{
				$search = $this->input->post('search');
			}
			$query_array = array(
				'search_by'=>$this->input->post('search_by'),
				'search'=>$search
			);
			$query_id=rand();
			$_SESSION['search']=$query_array;
			//echo '<pre>';print_r($_SESSION['search']);exit;
			redirect('admin/articles_ctrl/index/'.$query_id);
		}
		
		
		
		 function article_create_page(){
			//get-article_type:list
			$this->load->model('admin_articles_model');
			$data = array('www-command'=>'articles-get_articletype_list');
			$articletype_list = $this->admin_articles_model->articles_list($data);
			$data = array('www-command'=>'articles-get_authors_list');
			$authors_list = $this->admin_articles_model->articles_list($data);
			$data = array('www-command'=>'articles-get_disclaimers_list');
			$disclaimers_list = $this->admin_articles_model->articles_list($data);
			$data['articletype_list'] = $articletype_list;
			$data['authors_list'] = $authors_list;
			$data['disclaimers_list'] = $disclaimers_list;
			//echo '<pre>';print_r($data);exit;
			$this->load->view('admin/create_article', $data);
			//$this->load->view('admin/edit_article_page', $data);
		}
		
		function create_article(){
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('title', 'Article title', 'required|alpha|min_length[3]|max_length[20]');
				$this->form_validation->set_rules('type', '', 'callback_type');
				$this->form_validation->set_rules('author', '', 'callback_author');
				$this->form_validation->set_rules('disclaimer', '', 'callback_disclaimer');
				$this->form_validation->set_rules('pub_date', 'Publish date', 'required');
				$this->form_validation->set_rules('ex_date', 'Expiry date', 'required');
				$this->form_validation->set_rules('description', 'Description', 'required|max_length[255]');
					
				if($this->form_validation->run() == FALSE){
					$this->load->model('admin_articles_model');
					$data = array('www-command'=>'articles-get_articletype_list');
					$articletype_list = $this->admin_articles_model->articles_list($data);
					$data = array('www-command'=>'articles-get_authors_list');
					$authors_list = $this->admin_articles_model->articles_list($data);
					$data = array('www-command'=>'articles-get_disclaimers_list');
					$disclaimers_list = $this->admin_articles_model->articles_list($data);
					$data['articletype_list'] = $articletype_list;
					$data['authors_list'] = $authors_list;
					$data['disclaimers_list'] = $disclaimers_list;
						$this->load->view('admin/create_article',$data);
				}else{
					$url = CURL_URL;
					$data = array('www-command'=>'articles-create_article',
					'article_title' => $this->input->post('title'),
					'article_type_id' => $this->input->post('type'),
					'article_author_id' => $this->input->post('author'),
					'article_disclaimer_id' => $this->input->post('disclaimer'),
					'publish_date' => $this->input->post('pub_date'),
					'expiry_date' => $this->input->post('ex_date'),
					'description' => $this->input->post('description')
					);
				//echo '<pre>';print_r($data);exit;
				$string = http_build_query($data);
					//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//$data['articles_create_author'] = $response;
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/articles_ctrl/index/');
				}
			}
			
			function type($type){
				if($type == ''){
					$this->form_validation->set_message('type','Please select a article type');
					return false;
				}else{
					return true;
				}
			}
			
			function author($author){
				if($author == ''){
					$this->form_validation->set_message('author','Please select author name');
					return false;
				}else{
					return true;
				}
			}
			
			function disclaimer($disclaimer){
				if($disclaimer == ''){
					$this->form_validation->set_message('disclaimer','Please select disclaimer');
					return false;
				}else{
					return true;
				}
			}
			
			function edit_article_page(){
				$this->load->model('admin_articles_model');
				$data = array('www-command'=>'articles-get_articletype_list');
				$articletype_list = $this->admin_articles_model->articles_list($data);
				$data = array('www-command'=>'articles-get_authors_list');
				$authors_list = $this->admin_articles_model->articles_list($data);
				$data = array('www-command'=>'articles-get_disclaimers_list');
				$disclaimers_list = $this->admin_articles_model->articles_list($data);
				$data = array('www-command'=>'articles-get_article', 'article_id'=>$this->uri->segment(4));
				$articles_get_article = $this->admin_articles_model->articles_list($data);
				
				$data['articletype_list'] = $articletype_list;
				$data['authors_list'] = $authors_list;
				$data['disclaimers_list'] = $disclaimers_list;
				$data['articles_get_article'] = $articles_get_article;
				//echo '<pre>';print_r($data);exit;
				$this->load->view('admin/edit_article_page', $data);
			}
			
			function edit_article(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('title', 'Article title', 'required|alpha|min_length[3]|max_length[20]');
				$this->form_validation->set_rules('type', '', 'callback_type');
				$this->form_validation->set_rules('author', '', 'callback_author');
				$this->form_validation->set_rules('disclaimer', '', 'callback_disclaimer');
				$this->form_validation->set_rules('pub_date', 'Publish date', 'required');
				$this->form_validation->set_rules('ex_date', 'Expiry date', 'required');
				$this->form_validation->set_rules('description', 'Description', 'required|max_length[255]');
					
				if($this->form_validation->run() == FALSE){
					//echo $this->input->post('hidden');exit;
					$this->load->model('admin_articles_model');
				$data = array('www-command'=>'articles-get_articletype_list');
				$articletype_list = $this->admin_articles_model->articles_list($data);
				$data = array('www-command'=>'articles-get_authors_list');
				$authors_list = $this->admin_articles_model->articles_list($data);
				$data = array('www-command'=>'articles-get_disclaimers_list');
				$disclaimers_list = $this->admin_articles_model->articles_list($data);
				
				$data = array('www-command'=>'articles-get_article', 'article_id'=>$_SESSION['article_id']);
				$articles_get_article = $this->admin_articles_model->articles_list($data);
				
				$data['articletype_list'] = $articletype_list;
				$data['authors_list'] = $authors_list;
				$data['disclaimers_list'] = $disclaimers_list;
				$data['articles_get_article'] = $articles_get_article;
						//echo '<pre>';print_r($data);exit;
					$this->load->view('admin/edit_article_page', $data);
				}else{
					$url = CURL_URL;
					$data = array('www-command'=>'articles-update_article',
					'article_title' => $this->input->post('title'),
					'article_type_id' => $this->input->post('type'),
					'article_author_id' => $this->input->post('author'),
					'article_disclaimer_id' => $this->input->post('disclaimer'),
					'publish_date' => $this->input->post('pub_date'),
					'expiry_date' => $this->input->post('ex_date'),
					'description' => $this->input->post('description'),
					'article_id' => $this->input->post('hidden')
					);
					$string = http_build_query($data);
						//echo '<pre>';print_r($data);exit;
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//$data['articles_create_author'] = $response;
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/articles_ctrl/index/');
				}
		}
		
		function delete_article(){
			$url = CURL_URL;
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'articles-delete_article','status'=>$status,'article_id'=>$this->uri->segment(4));
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				
				redirect('admin/articles_ctrl/index/');

		}
		
		
	
	}
	
?>